package com.ubao.ui.pullToRefreshListView.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ubao.R;
import com.ubao.bean.Following;
import com.ubao.util.log.Logger;

public class FollowingsAdapter extends BaseAdapter {
	private Context context;
	private List<Following> followings;
	private LayoutInflater ll;
	String count[];
	//private DatabaseHelper dbHelper;
	private Boolean isClicked = false;

	public FollowingsAdapter(Context context) {
		this.context = context;
		followings = new ArrayList<Following>();
		ll = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}

	private void getMyCount() {
//		dbHelper = new DatabaseHelper(context);
//		if (dbHelper.query() != null && dbHelper.query().length > 0) {
//			count = dbHelper.query();
//		}
//		dbHelper.close();
	}

	// 设置ListView的底层数据，并通知ListView刷新界面
	public void setData(List<Following> news) {
		
		this.followings = news;
		getMyCount();
		this.notifyDataSetChanged();
	}

	// 新添加的数据，追加到List的最后
	public void appendData(List<Following> subNews) {
		this.followings.addAll(subNews);
		Logger.d("add" + followings.size());
		getMyCount();
		this.notifyDataSetChanged();
	}
	
	public void notifyDatabaseChanged(){
		getMyCount();
		this.notifyDataSetChanged();
	}

	public List<Following> getData() {
		return followings;
	}

	@Override
	public int getCount() {
		return followings.size();
	}

	@Override
	public Object getItem(int position) {
		return followings.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// LinearLayout linearLayout =
		// (LinearLayout)ll.inflate(R.layout.item_list_financenews, null);
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = ll.inflate(R.layout.item_myfriends, null);
			vh.newsTitle = (TextView) convertView.findViewById(R.id.tvName);
			vh.newsDate = (TextView) convertView.findViewById(R.id.tvDesc);
			convertView.setTag(vh);
		} else{
			vh = (ViewHolder) convertView.getTag();
		}
		vh.newsTitle.setText(followings.get(position).getUserID());
		vh.newsDate.setText(followings.get(position).getCreateTime());
		return convertView;
	}

	private CharSequence doCut(String title) {
		if(title.length()>28){
			title = title.substring(0, 28) + "...";
		}
		return title;
	}

	// 不用再次解析Layout文件中的控件
	public static class ViewHolder {
		public TextView newsTitle;
		public TextView newsDate;
	}

}
